/****************************************************************************************************************************
* The original serial port listener by Sun was modified to read and interpret data from a Basic Stamp II. The basic stamp	* 
* has been programmed to allow input from 12 different buttons. The BS2 then sends the data in binary form to the serial 	*
* port. Only 10 inputs are used however, 5 on each hand.																	*
*																															*
* Originaly written by John Hopf																							*
* 																															*
* Editing and name changed by Ross Smith - Jan 2005																			*
* The number of bytes read nust be two, if any other number is read we ignore the results.  Before this was done there would*
* be erratic inaccurate results causing the world to float.																	*
*																															*
****************************************************************************************************************************/

import java.io.*;
import javax.comm.SerialPort;
import javax.comm.SerialPortEventListener;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPortEvent;

public class ButtonDeviceSerialPort implements SerialPortEventListener {
  
	/*Enable/Disable debugging oupput*/
	private boolean DEBUG = false;
  
	/*Button states*/
	private boolean button_1_down = false;
	private boolean button_2_down = false;
	private boolean button_3_down = false;
	private boolean button_4_down = false;
	private boolean button_5_down = false;
	private boolean button_6_down = false;
	private boolean button_7_down = false;
	private boolean button_8_down = false;
	private boolean button_9_down = false;
	private boolean button_10_down = false;
  
	/*Serial Port Identifiers*/
	private InputStream inputStream;
	private SerialPort serialPort;
	static CommPortIdentifier portId;
   
	public ButtonDeviceSerialPort(CommPortIdentifier der) {
         
		ButtonDeviceSerialPort.portId = der;
         try {
            /*Set up the serial port*/
         	serialPort = (SerialPort) portId.open("MouseDeviceSerialPortApp", 2000);
            inputStream = serialPort.getInputStream();
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.setDTR(false); /*Set to false while not programming basic stamp 2*/       
         } 
         catch (Exception e) {
         	e.printStackTrace();
         }
	}
   
	public void serialEvent(SerialPortEvent event) {
		switch(event.getEventType()) {
			case SerialPortEvent.DATA_AVAILABLE:
               
      			byte[] readBuffer = new byte[200];
               	int numBytes = 0;
            
               	try {
               		while (inputStream.available() > 0) {
	                    numBytes = inputStream.read(readBuffer,0,2);
	                    
	                    /*Only look at data if the record is 3 bytes long, may ocasionaly be one byte*/
	                    if(numBytes == 2){
	                    	
	                    	/*Binary to ascii function for debygging*/
	                    	if(DEBUG)	
	                    		toBinary(readBuffer[0]);
	                  
	                    	/*Get all button Information and assign to buffer*/
	                    	if(((1 << 0) & readBuffer[0]) != 0)
	                    		button_1_down = true;
	                    	else
	                    		button_1_down = false;
	                  
	                    	if(((1 << 1) & readBuffer[0]) != 0)
	                    		button_2_down = true;
	                    	else
	                    		button_2_down = false;
	                  
	                    	if(((1 << 2) & readBuffer[0]) != 0)
	                    		button_3_down = true;
	                    	else
	                    		button_3_down = false;
	                  
	                    	if(((1 << 3) & readBuffer[0]) != 0)
	                    		button_4_down = true;
	                    	else
	                    		button_4_down = false; 
	                  
	                    	if(((1 << 4) & readBuffer[0]) != 0)
	                    		button_5_down = true;
	                    	else
	                    		button_5_down = false; 
	                  
	                    	if(((1 << 5) & readBuffer[0]) != 0)
	                    		button_6_down = true;
	                    	else
	                    		button_6_down = false; 
	                  
	                    	if(((1 << 6) & readBuffer[0]) == 0)
	                    		button_7_down = false;
	                    	else
	                    		button_7_down = true; 
	                  
	                    	if(((1 << 7) & readBuffer[0]) != 0)
	                    		button_8_down = true;
	                    	else
	                    		button_8_down = false; 
	                  
	                    	if(((1 << 0) & readBuffer[1]) != 0)
	                    		button_9_down = true;
	                    	else
	                    		button_9_down = false; 
	                  
	                    	if(((1 << 1) & readBuffer[1]) != 0)
	                    		button_10_down = true;
	                    	else
	                    		button_10_down = false; 
	                    }
               		}
               	} 
                catch (IOException e) {
                	e.printStackTrace();
                }            
                break;
           }
      }
   
    /*convert the integer parameter into 16 bit binary*/
	public void toBinary (int num) {
		for(int i = 15; i >= 0; i--) {
			if(((1 << i) & num) != 0)
				System.out.print("1");
            else
            	System.out.print("0");
         }
         System.out.println(" ");
    }
   
   	/* return status of button 1*/
	public boolean getButton1() {
		if(DEBUG)
			System.out.println(button_1_down);
        return button_1_down;
	}
   
	/* return status of button 2*/
    public boolean getButton2() {
    	if(DEBUG)
    		System.out.println(button_2_down);
        return button_2_down;
    }
   
    /* return status of button 3*/
    public boolean getButton3() {
    	if(DEBUG)
    		System.out.println(button_3_down);
    	return button_3_down;
    }
   
    /* return status of button 4*/
    public boolean getButton4() {
    	if(DEBUG)
    		System.out.println(button_4_down);
        return button_4_down;
    }
   
    /* return status of button 5*/
    public boolean getButton5() {
    	if(DEBUG)
    		System.out.println(button_5_down);
        return button_5_down;
    }
   
    /* return status of button 6*/
    public boolean getButton6() {
    	if(DEBUG)
    		System.out.println(button_6_down);
    	return button_6_down;
    }
   
    /* return status of button 7*/
    public boolean getButton7() {
    	if(DEBUG)
    		System.out.println(button_7_down);
    	return button_7_down;
    }
   
    /* return status of button 8*/
    public boolean getButton8() {
    	if(DEBUG)
    		System.out.println(button_8_down);
    	return button_8_down;
    }
   
    /* return status of button 9*/
    public boolean getButton9() {
    	if(DEBUG)
    		System.out.println(button_9_down);
    	return button_9_down;
    }
   
   	/* return status of button 10*/
    public boolean getButton10() {
    	if(DEBUG)
    		System.out.println(button_10_down);
    	return button_10_down;
    }
}
